// pages/login/login.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scend:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options.scene){
      this.setData({
        scend: options.scene
      })
    }else{
    
    }
  },

  login:function(e){
   

    if (e.detail.userInfo){

      console.log(e)


      wx.setStorageSync("encryptedData", e.detail.encryptedData)
      wx.setStorageSync("iv", e.detail.iv)

      var that=this
      var code = wx.getStorageSync("code");
      if (that.data.scend!=''){
        ajax('/wxLogin', {
          data: {
            code: code,
            scene: that.data.scend,
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
          method: 'post'
        }, res => {
          console.log(res)
          console.log(res)
          if (res.status == 200) {
            wx.setStorageSync('isAudit', '11111')
            console.log('重设')
            

            wx.setStorageSync("token", res.data.token)
            wx.setStorageSync("sessionKey", res.data.sessionKey)

            wx.setStorageSync("isFirst", res.data.isFirst)

            ajax('/wxUserInfo', {
              data: {
                sessionKey: wx.getStorageSync("sessionKey"),
                encryptedData: wx.getStorageSync("encryptedData"),
                iv: wx.getStorageSync("iv"),
              },
              header: {
                'Accept': 'application/json', // 默认值
              },
              method: 'post'
            }, res => {
              console.log(res)
              console.log(res)
              if (res.status == 200) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })

           
          }
        })
      }else{
        ajax('/wxLogin', {
          data: {
            code: code,
            
          },
          header: {
            'Accept': 'application/json', // 默认值
          },
          method: 'post'
        }, res => {
          console.log(res)
          console.log(res)
          if (res.status == 200) {

            wx.setStorageSync('isAudit', '11111')
            console.log('重设')
          

            wx.setStorageSync("token", res.data.token)
            wx.setStorageSync("sessionKey", res.data.sessionKey)

            wx.setStorageSync("isFirst", res.data.isFirst)

            ajax('/wxUserInfo', {
              data: {
                sessionKey: wx.getStorageSync("sessionKey"),
                encryptedData: wx.getStorageSync("encryptedData"),
                iv: wx.getStorageSync("iv"),
              },
              header: {
                'Accept': 'application/json', // 默认值
              },
              method: 'post'
            }, res => {
              console.log(res)
              console.log(res)
              if (res.status == 200) {
                wx.switchTab({
                  url: '/pages/index/index',
                })
              }
            })
          }
        })
      }
    }else{
      wx.showToast({
        title: '请先授权！',
        icon: 'none',
        duration: 2000
      })
    }
  },

  onPageScroll: function (e) {
    if (e.scrollTop < 0) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        wx.setStorageSync("code", res.code)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})